package com.eduService.teacher.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eduService.teacher.entity.EduTeacher;
import com.eduService.teacher.service.IEduTeacherService;
import com.eduService.teacher.utils.R;
import com.eduService.teacher.vo.teacherConditionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Tangcow
 * @since 2022-03-26
 */
@RestController
@RequestMapping("/eduteacher")
@CrossOrigin
public class EduTeacherController {
    @Autowired
    IEduTeacherService iEduTeacherService;

    @GetMapping("/findall")
    public R findallTeacher(){
        return R.ok().data("allteacher",iEduTeacherService.list(null));
    }

    @GetMapping("pageteacher/{page}/{limit}")
    public R PageTeacher(@PathVariable("page") Long page , @PathVariable("limit") Long limit){
        Page<EduTeacher> tpage = new Page<>(page,limit);
        iEduTeacherService.page(tpage,null);
        List<EduTeacher> records = tpage.getRecords();
        long total = tpage.getTotal();
        return R.ok().data("total",total).data("data",records);
    }

    @PostMapping("pageteachercondition/{page}/{limit}")
    public R pageTeacherCondition(@PathVariable("page") Long page , @PathVariable("limit") Long limit, @RequestBody  teacherConditionVo teacherVo){
        Page<EduTeacher> tpage = new Page<>(page,limit);
        iEduTeacherService.pageQuery(tpage,teacherVo);
        List<EduTeacher> records = tpage.getRecords();
        long total = tpage.getTotal();
        return R.ok().data("total",total).data("data",records);
    }

    @DeleteMapping("delteacher/{id}")
    public R delTeacher(@PathVariable("id") String id){
        boolean isDel = iEduTeacherService.removeById(id);
        if (isDel){
            return  R.ok().msg("删除成功！");
        }
        return R.error().msg("删除失败！");
    }

    @PostMapping("/save")
    public R saveTeacher(@RequestBody EduTeacher eduTeacher){
        System.out.println(eduTeacher.toString());
        if(iEduTeacherService.save(eduTeacher)){
            return R.ok().msg("增加一条讲师信息！");
        }else{
            return R.error().msg("添加失败！");
        }
    }

    @GetMapping("queryteacher/{id}")
    public R queryTeacherById(@PathVariable("id") String id){
        return R.ok().data("teacher",iEduTeacherService.getById(id));
    }

    @PostMapping("updateTeacher")
    public R updateTeacherByid(@RequestBody EduTeacher teacher){
        if(iEduTeacherService.updateById(teacher)){
            return R.ok().msg("修改成功！");
        }else{
            return R.error().msg("修改失败！");
        }
    }

}
